/* *
 * 通用公共js库，主要定义api接口，auth apikey 常量值和通用函数
 */
// api接口地址
import Vue from 'vue'
import defaultData from './default.js';
// const verurl = defaultData.baseUrl +'/fenxiao/mall-web'; 


export const Request=(config)=> { 
	let url = config.url;
	let method = config.method;
	let data = config.data || {};
	let loadStatus = config.loadStatus || false;//是否显示加载动画
	let header = config.header || {'Content-Type': 'application/x-www-form-urlencoded'};
	let tokenValue = uni.getStorageSync("token");//在本地存储中获取token
	console.log();
	if (tokenValue) {
		if (!header || !header['Token']) {
			header['Token'] = tokenValue;
		}
	}
	uni.showLoading({
	  title: '加载中',
	  mask:true
	})
	let promise = new Promise(function(resolve, reject) {
		uni.request({
			url: url,
			header: header,
			data: data,
			method: method,
			success: function(res) {
				if(res.data.code == '200'||res.data.code == '0'){
					uni.hideLoading()
					resolve(res.data);
				}else{
					let _date = new Date();
					reject(res.data);
					console.log('▶请求接口：【'+config.url+'】' + ' ▶请求时间：【' + _date +'】'+ ' ▶报错信息：【' + (!!res.message?res.message:JSON.stringify(res.data))+'】' );
				}
			},
			fail: function(err){
                let _date = new Date();
				console.log('▶请求接口：【'+config.url+'】' + ' ▶请求时间：【' + _date +'】'+ ' ▶报错信息：【' + (!!err.message?err.message:JSON.stringify(err.data))+'】' );
				reject(err.data);
			},
			complete: function() {
				if(loadStatus){
					console.log('in')
				}else{
					// uni.hideLoading()
				}
				
			}
		});
	});
	return promise;
}

export default {
	Request
};
